package com.example.ole.aspectjbase.aop;

import android.util.Log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class AspectA {
    public String TAG = "AspectA";

    @Before("execution(* com.example.ole.aspectjbase.A.B(..))")
    public void pointCut1(JoinPoint joinPoint) {
        Log.e(TAG, "Before A::B");
    }

    @After("execution(* com.example.ole.aspectjbase.A.B(..))")
    public void pointCut2(JoinPoint joinPoint) {
        Log.e(TAG, "After A::B");
    }

    @Around("call(* com.example.ole.aspectjbase.A.B(..))")
    public void pointCut3(ProceedingJoinPoint processdingJP) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) processdingJP.getSignature();
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();
        String arg = (String) processdingJP.getArgs()[0];


        Log.e(TAG, "Around" + className + "." + methodName + " + " + arg);
        long start = System.currentTimeMillis();

        arg += " world";
        Object ret = processdingJP.proceed(new Object[]{arg});

        long end = System.currentTimeMillis();
        Log.e(TAG, "Around" + className + "." + methodName + " - " + (end - start));
    }
}
